
package kr.co.mcshop.basket;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import kr.co.mcshop.product.ProductService;
import kr.co.mcshop.user.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping(value="/basket")
@Controller
public class basketController {
        
        @Autowired
        basketService basketservice;
        
        @Autowired
        ProductService ps;
        
        @RequestMapping(value="/basketAdd")
        public String basketAdd(@RequestParam Map map, HttpSession session){
                User user = (User) session.getAttribute("loginUser");
                map.put("user_no", user.getNo());
                System.out.println(map);
                
                basketservice.insertBasket(map);
                return "redirect:/";
        }
        
        @RequestMapping(value="/basketList")
        public String basketList(@RequestParam Map map, Model model){
                List<Map> basketList = basketservice.getAllBasket(map);
                for(int i=0; i<basketList.size(); i++){
                        HashMap<Object, Object> hashmap = new HashMap<Object, Object>();
                        hashmap.put("no", basketList.get(i).get("product_no"));
                        List<Map> productList = ps.getAllProduct(hashmap);
                        basketList.get(i).putAll(productList.get(0));
                        System.out.println(basketList);
                }
                
                
                model.addAttribute("basketList", basketList);
                return "basket/basketList";
        }
}